# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=mdadm
pkgver=4.3
pkgrel=1
pkgdesc="a tool for managing Linux Software RAID arrays"
url="https://raid.wiki.kernel.org/index.php/A_guide_to_mdadm"
arch="all"
license="GPL-2.0-only"
depends="lsblk"
makedepends="eudev-dev groff linux-headers"
checkdepends="bash eudev e2fsprogs"
subpackages="$pkgname-doc $pkgname-udev::noarch $pkgname-misc::noarch $pkgname-openrc"
source="https://www.kernel.org/pub/linux/utils/raid/mdadm/mdadm-$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname-raid.initd
	basename.patch
	Create.c-fix-build.patch
	util.c-include-limits.h.patch
	lfs64.patch
	no-werror.patch
	mdadm-include-sysmacros.patch
	fix-monitor-tv_sec.patch
	"
options="!check"

build() {
	make CXFLAGS="$CFLAGS -DNO_LIBUDEV"
	make raid6check
}

# This doesn't build in Alpine CI. But it should work on bare-metal as root.
check() {
	export PATH="$PWD:$PATH" # Test needs built binaries
	make test
	./test
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm755 raid6check "$pkgdir"/sbin/raid6check
	install -Dm644 raid6check.8 "$pkgdir"/usr/share/man/man8/raid6check.8

	install -Dm644 $pkgname.conf-example "$pkgdir"/etc/$pkgname.conf
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname-raid.initd "$pkgdir"/etc/init.d/$pkgname-raid
}

udev() {
	pkgdesc="$pkgdesc (udev rules)"
	install_if="udev $pkgname=$pkgver-r$pkgrel"

	amove usr/lib/udev
}

misc() {
	pkgdesc="$pkgdesc (misc scripts)"
	depends="$pkgname bash"

	cd "$builddir"

	install -Dm755 misc/mdcheck "$subpkgdir"/usr/sbin/mdcheck
	install -Dm755 misc/syslog-events "$subpkgdir"/usr/sbin/handle-mdadm-events
}

sha512sums="
a77af2b371ed27b7bd89b7991302282f0fc77999eb93d7959446fc4b1c28388582d08ff0abfc470a9f9a8c8036acb0158edc44d9a8d406f807ca28111bf74459  mdadm-4.3.tar.gz
ca5f4e3ff5b284102b44e818d11622e1889066e3d18edce2d62c1a44ee8f4cfdc0979121c0462a916c638423c5ebc706c46aa996a7c4f68e030589adc62803f4  mdadm.initd
7d45bf489ef93a4f217ffcf72311eb661b8de7fbf63a5344697252c0d3a684b0123ff60efa5f218da4eb4cda7c88d91c2ef3625d5e44a588e3e1210cb60b0ab9  mdadm.confd
10d2456ca82c353c9b8540ca996de48575a60b83ede5b0bb7e960b4dd28aac2aa73eaeff4bdb99d03ce9630f986c2023b38fe332c5be74e0234985af6273a934  mdadm-raid.initd
cb980a8f0249aa56e01afb34f4e456a1486dd32aca418f723277ffb1e30d311e27f54991d6c82cc02b2f4e6d98c34f2fbaf96b416414dd32bdc2478a75c9f71a  basename.patch
40c1935a0f0491bac6b33205883c1920b4ce5830f100462ae448f8f34d143d0de716007d6b14a207879192c73c10141e7ecf414a866b51cf52652950d781e0cc  Create.c-fix-build.patch
6513bef2ccf2bfa9caa536f1475161f6a207f202be48dde4d4516b5817f03e1da77cdbf646666d39729a34296ed14413348518f96727926e8b7398f17c8d9e2a  util.c-include-limits.h.patch
0516df3117618e46b9699d427f3c3601701906bbdb1b2f8fb913e443c21f0fba021e66fb352f9d97678e1394e05c9f4fd041fc3f1f14953d219bbcd804eb6d55  lfs64.patch
e01f1eeb35c0890505004f1e35c8f73de5d1b406c64bc24d0d8577ab3fdbd60b1b912f9321e266a298251cbd92f0fe35a8663d174eedd4c77609e1613c57368c  no-werror.patch
40994227cb4cab3cd890e674ad78cb1fd1d5dcb4a577c86dc1948d465fe5e95092809788bf97c917cee038f5be1cd376c453e69dc28ecb6accb3a1aa3d79c01d  mdadm-include-sysmacros.patch
5d337f39d8f9b95447d4b6e0b9b1442a9893ea7f47b25687df0c7ad67a47f151900bfbcbd4ebf43ee2833f8643696df163b931c389b0a6498be7e2377913b965  fix-monitor-tv_sec.patch
"
